package algdat;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;

/**
 * @author Mikko Puustinen (mikko.puustinen@gmail.com)
 * @version 0.1, 2007-nov-11
 */
public class Ovning35 {

    private Person readFile() {
        Person[][] parr;
        Scanner scanner = null;
        String[] cols;
        int total = 0;

        try {
            scanner = new Scanner(new FileReader("Lista1.dat"));
        } catch (FileNotFoundException e) {
            //e.printStackTrace();
        }

        //Rad 1 = Antalet vertikala listor
        //Rad 2 = Vilka element som ska vara i vilken lista, space-separerad
        //Rad 3 = Totala antalet namn

        assert scanner != null : "Not good";

        scanner.nextLine();
        cols = scanner.nextLine().split(" ");
        total = Integer.parseInt(scanner.nextLine());
        parr = new Person[cols.length][total];

        for (int i = 0; i < cols.length; i++) {
            for (int j = 0; j < Integer.parseInt(cols[i]); j++) {
                parr[i][j] = new Person(scanner.nextLine());
                if (j > 0)
                    parr[i][j - 1].down = parr[i][j];
            }
            if (i > 0)
                parr[i - 1][0].right = parr[i][0];
        }

        /*for (int i = 0; i < cols.length; i++) {
            for (int j = 0; parr[i][j] != null; j++) {
                System.out.print("Name: " + parr[i][j].name);

                if (parr[i][j].down != null) {
                    System.out.print(" , down: " + parr[i][j].down.name);
                }

                if (parr[i][j].right != null) {
                    System.out.print(", right: " + parr[i][j].right.name);
                }
                System.out.print("\n");
            }
            System.out.println("-------");
        }*/

        scanner.close();

        return parr[0][0];
    }

    private void printNames(Person start) {
        Person currTop = start;
        Person currDown = currTop;

        while (currTop != null) {
            while (currDown.down != null) {
                currDown = currDown.down;
            }
            System.out.println(currDown.name);
            currTop = currTop.right;
            currDown = currTop;
        }
    }

    private Ovning35() {
        Person start = readFile();
        printNames(start);
    }

    public static void main(String[] args) {
        new Ovning35();
    }

    private class Person {
        String name;
        Person right;
        Person down;

        private Person(String name) {
            this.name = name;
        }
    }
}
